package com.atguigu.mvc.controller;

import jakarta.annotation.Resource;
import jakarta.servlet.ServletContext;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Map;

@Controller
public class Demo06ControllerAttr {

    @Resource
    private ServletContext servletContext;

    @RequestMapping("/test/request/attr/model")
    public String testRequestAttr(Model model) {

        // 把数据存入请求域
        model.addAttribute("message", "hello, spring mvc");

        return "page02target";
    }

    @RequestMapping("/test/request/attr/model/map")
    public String testRequestAttrModelMap(ModelMap modelMap) {

        modelMap.addAttribute("message", "hello, spring mvc(Model map)");

        return "page02target";
    }

    @RequestMapping("/test/request/attr/map")
    public String testRequestAttrModelMap(Map<String, Object> map) {

        map.put("message", "hello, spring mvc(Map)");

        return "page02target";
    }

    @RequestMapping("/test/request/attr/request")
    public String testRequestAttrModelMap(HttpServletRequest request) {

        request.setAttribute("message", "hello, spring mvc(request)");

        return "page02target";
    }

    @RequestMapping("/test/session/attr")
    public String testSessionAttr(HttpSession session) {

        session.setAttribute("loginUser", "User Good");

        return "page02target";
    }

    @RequestMapping("/test/application/attr")
    public String testSessionAttr() {

        servletContext.setAttribute("onlineUserCount", "10万+");

        return "page02target";
    }
}
